package com.rogan.cg;

import java.lang.reflect.Method;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

public class ProxyFactory implements MethodInterceptor{

	private Object target;
	
	public ProxyFactory(Object target) {
		this.target = target;
	}
	
	public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
		System.out.println("start");
		Object returnValue = method.invoke(this.target, args);
		System.out.println("end");
		return returnValue;
	}

	 public Object getProxyInstance() {
		// 1 工具类
		Enhancer en = new Enhancer();
		// 2 设置父类
		en.setSuperclass(this.target.getClass());
		// 3. 设置回调函数
		en.setCallback(this);
		// 4 创建子类（代理对象）
		return en.create();
	 }
}
